﻿Public Class frmConfigurarEscaner
    Dim beEscaner As New BEL.EscanerBE
    Dim bllEscaner As New BLL.EscanerBL
    Private Sub frmConfigurarEscaner_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Call cargarEscaners()

    End Sub
    Sub cargarEscaners()
        Dim _DataTableEscaners As DataTable = bllEscaner.GetListAll
        lstEscaners.Items.Clear()

        Dim lviItem As ListViewItem
        For Each dtrFila As DataRow In _DataTableEscaners.Rows
            lviItem = New ListViewItem
            lviItem.Text = dtrFila("IdEscaner")
            lviItem.SubItems.Add(dtrFila("Nombre"))
            lviItem.SubItems.Add(dtrFila("Descripcion"))
            lviItem.SubItems.Add(dtrFila("FechaCreacion"))

            lviItem.SubItems.Add(dtrFila("TipoArchivo"))

            lviItem.SubItems.Add(dtrFila("Resolucion"))
            lviItem.SubItems.Add(dtrFila("Duplex"))
            lviItem.SubItems.Add(dtrFila("Formato"))
            lviItem.SubItems.Add(dtrFila("EliminarDesviacion"))
            lviItem.SubItems.Add(dtrFila("EliminarBlancos"))

            lviItem.ImageIndex = 1

            lstEscaners.Items.Add(lviItem)
        Next
    End Sub

    Private Sub lstEscaners_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstEscaners.DoubleClick
        If lstEscaners.SelectedItems.Count = 0 Then Exit Sub
        Call ModificaEscaner(lstEscaners.SelectedItems(0).Text)
    End Sub
    Private Sub ModificaEscaner(ByVal IdEscaner As Integer)
        Dim frmEscaner As New frmAddConfigurarEscaner
        frmEscaner.IdEscaner = IdEscaner
        frmEscaner.ShowDialog()

        Call cargarEscaners()

    End Sub
    Private Sub lstEscaners_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstEscaners.SelectedIndexChanged
        If lstEscaners.SelectedItems.Count = 0 Then Exit Sub

        Select Case lstEscaners.SelectedItems(0).SubItems(7).Text
            Case "0"
                lblTipoImagen.Text = "Blanco y negro"
            Case "1"
                lblTipoImagen.Text = "Escala de grises"
            Case "2"
                lblTipoImagen.Text = "Color"
        End Select

        lblResolucion.Text = lstEscaners.SelectedItems(0).SubItems(5).Text
        If lstEscaners.SelectedItems(0).SubItems(4).Text = "PDO" Then
            LblFormato.Text = "PDF con OCR"
        Else
            LblFormato.Text = lstEscaners.SelectedItems(0).SubItems(4).Text
        End If

        lblDuplex.Text = lstEscaners.SelectedItems(0).SubItems(6).Text
        LblDesviacion.Text = lstEscaners.SelectedItems(0).SubItems(8).Text
        lblPaginasBlanco.Text = lstEscaners.SelectedItems(0).SubItems(9).Text
    End Sub

    Private Sub LnkAdd_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LnkAdd.LinkClicked
        Dim frmAddEscaner As New frmAddConfigurarEscaner
        frmAddEscaner.ShowDialog()

        Call cargarEscaners()
    End Sub

    Private Sub LnkModificarTypDoc_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LnkModificarTypDoc.LinkClicked
        If lstEscaners.SelectedItems.Count = 0 Then
            MsgBox("Seleccione la configuración de escaner que desea modificar.", MsgBoxStyle.Information, Me.Text)
            Exit Sub
        End If
        Call ModificaEscaner(lstEscaners.SelectedItems(0).Text)

    End Sub

    Private Sub LnkEliminar_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LnkEliminar.LinkClicked
        If lstEscaners.SelectedItems.Count = 0 Then
            MsgBox("Seleccione la configuración de escaner que desea Eliminar.", MsgBoxStyle.Information, Me.Text)
            Exit Sub
        End If

        If MsgBox("Esta seguro de eliminar esta configuración?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, Me.Text) = MsgBoxResult.Yes Then
            Dim StrMensaje As String
            StrMensaje = bllEscaner.Eliminar(lstEscaners.SelectedItems(0).Text)
            If StrMensaje = "OK" Then
                MsgBox("Registro eliminado correctamente.", MsgBoxStyle.Information, Me.Text)
            Else
                MsgBox("Error al eliminar registro." & vbCrLf & StrMensaje, MsgBoxStyle.Information, Me.Text)
            End If
            Call cargarEscaners()
        End If


    End Sub
End Class